package com.hainatong.shiphelper.utils;


import com.hainatong.shiphelper.R;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

public class DialogUitl
{

    public static void showSimpleDialog(Context context, String content, SimpleCallback callback)
    {
        showSimpleDialog(context, content, true, callback);
    }

    public static void showSimpleDialog(Context context, String content, boolean cancelable, SimpleCallback callback)
    {
        showSimpleDialog(context, null, content, cancelable, callback);
    }

    public static void showSimpleDialog(Context context, String title, String content, boolean cancelable, SimpleCallback callback)
    {
        new Builder(context)
                .setTitle(title)
                .setContent(content)
                .setCancelable(cancelable)
                .setClickCallback(callback)
                .build()
                .show();
    }


    public static class Builder {

        private Context mContext;
        private String mTitle;
        private String mContent;
        private String mConfrimString;
        private String mCancelString;
        private boolean mCancelable;
        private boolean mBackgroundDimEnabled;//显示区域以外是否使用黑色半透明背景

        private String mHint;

        private SimpleCallback mClickCallback;

        public Builder(Context context)
        {
            mContext = context;
        }

        public Builder setTitle(String title)
        {
            mTitle = title;
            return this;
        }

        public Builder setContent(String content)
        {
            mContent = content;
            return this;
        }

        public Builder setConfrimString(String confrimString)
        {
            mConfrimString = confrimString;
            return this;
        }

        public Builder setCancelString(String cancelString)
        {
            mCancelString = cancelString;
            return this;
        }

        public Builder setCancelable(boolean cancelable)
        {
            mCancelable = cancelable;
            return this;
        }

        public Builder setBackgroundDimEnabled(boolean backgroundDimEnabled)
        {
            mBackgroundDimEnabled = backgroundDimEnabled;
            return this;
        }

        public Builder setHint(String hint)
        {
            mHint = hint;
            return this;
        }

        public Builder setClickCallback(SimpleCallback clickCallback)
        {
            mClickCallback = clickCallback;
            return this;
        }

        public Dialog build()
        {
            final Dialog dialog = new Dialog(mContext, mBackgroundDimEnabled ? R.style.main_dialog : R.style.main_dialog2);
            dialog.setContentView(R.layout.main_dialog_simple);
            dialog.setCancelable(mCancelable);
            dialog.setCanceledOnTouchOutside(mCancelable);
            TextView titleView = (TextView) dialog.findViewById(R.id.title);
            if (!TextUtils.isEmpty(mTitle))
            {
                titleView.setText(mTitle);
            }
            final TextView content = (TextView) dialog.findViewById(R.id.content);
            if (!TextUtils.isEmpty(mHint))
            {
                content.setHint(mHint);
            }
            if (!TextUtils.isEmpty(mContent))
            {
                content.setText(mContent);
            }

            TextView btnConfirm = (TextView) dialog.findViewById(R.id.btn_confirm);
            if (!TextUtils.isEmpty(mConfrimString))
            {
                btnConfirm.setText(mConfrimString);
            }
            TextView btnCancel = (TextView) dialog.findViewById(R.id.btn_cancel);
            if (!TextUtils.isEmpty(mCancelString)) {
                btnCancel.setText(mCancelString);
            }
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if (v.getId() == R.id.btn_confirm)
                    {
                        if (mClickCallback != null)
                        {
                            mClickCallback.onConfirmClick(dialog, "");
                            dialog.dismiss();
                        }
                        else
                        {
                            dialog.dismiss();
                        }
                    } else {
                        dialog.dismiss();
                        if (mClickCallback instanceof SimpleCallback2) {
                            ((SimpleCallback2) mClickCallback).onCancelClick();
                        }
                    }
                }
            };
            btnConfirm.setOnClickListener(listener);
            btnCancel.setOnClickListener(listener);
            return dialog;
        }

    }

    public interface SimpleCallback
    {
        void onConfirmClick(Dialog dialog, String content);
    }

    public interface SimpleCallback2 extends SimpleCallback
    {
        void onCancelClick();
    }

    public static void showUpdateDialog(final Context context, boolean isForce )
    {
        DialogUitl.Builder builder = new DialogUitl.Builder(context);
        builder.setTitle("版本更新")
                .setContent("已检查到新版本!")
                .setConfrimString("立即更新")
                .setCancelString("暂不更新")
                .setCancelable(true)
                .setClickCallback(new DialogUitl.SimpleCallback() {
                    @Override
                    public void onConfirmClick(Dialog dialog, String content)
                    {
                        //DownloadUtils.getInstance().downloadAPK(context, ConstantInfo.DOWNLOAD_URL ,"kftx.apk");
                    }
                })
                .build()
                .show();
    }

}
